// pages/user_page/user_page.js
const SERVER = require("../../interfaces/interface")
const VEH = require("../../interfaces/vehicles")
const ORDER = require("../../interfaces/order")
let app = getApp()
Page({
  data: {
    has_user_info: false,
    has_account_info: false,
    account_info: {
      nickName: "Sample",
      account: "need2login@example.com",
      n_orders: 0,
      n_vehicles: 0,
      avatar: "https://i2.hdslb.com/bfs/face/61b39bcde88be469dfd1beb8e8b33858eda401f6.jpg"
    },
  },

  onLoad() {
    
  },

  onShow() {
    this.updateData()
    getApp().refreshAccountInfo()
  },

  need2hloginHandle(e){
    let kind = e.currentTarget.dataset.kind
    if(kind=="login")
    {
      if(SERVER.has_login())
        return
      this.loginHandler()
    }
    else
    {
      if(!SERVER.has_login())
        return
      if(kind=="order")
      {
        this.jump2order()
      }
      else if(kind=="vehicle")
      {
        this.jump2vehicle()
      }
      else if(kind=="logout")
      {
        this.logout()
      }
    }
  },

  loginHandler() {
    getApp().loginJump()
  },

  jump2order() {
    wx.navigateTo({
      url: '../order/order',
    })
  },

  jump2vehicle() {
    wx.navigateTo({
      url: '../vehicle/vehicle',
    })
  },

  jump2theme(e) {
    wx.navigateTo({
      url: '../theme/theme',
    })
  },

  async jump2user_opt(e) {
    wx.navigateTo({
      url: '../user_opt/user_opt',
    })
  },

  logout(e) {
    wx.showModal({
      title: "是否退出登录？",
      content: "退出登录后需要重新输入密码以登录",
      success: (res) => {
        if (res.confirm) {
          SERVER.logout()
          app.logout()
          this.updateData()
        }
      }
    })
  },

  onPullDownRefresh() {
    this.updateData()
  },

  updateData() {
    let global_account_info = getApp().globalData.account_info
    this.setData({
      ["account_info.nickName"]: global_account_info.nickName,
      ["account_info.account"]:global_account_info.account,
      ["account_info.n_vehicles"]: VEH.getVehicleNum(),
      ["account_info.n_orders"]: ORDER.getOrderNum()
    })
  }
})